// 星星组件公用方法
function convertToStarsArray(stars) {
  // stars为从豆瓣中获取的30,50字符串
  // 获取数据中的评分截取第一个数字
  var num = stars.toString().substring(0, 1);
  // 定义一个数组[1,1,1,1,0]表示四颗星
  var array = [];
  // 循环五次
  for (var i = 1; i <= 5; i++) {
    if (i <= num) {
      array.push(1);
    } else {
      array.push(0);
    }
  }
  return array;
}

function http(url, callBack) {
  wx.request({
    url: url,
    method: 'GET',
    header: {
      'content-type': 'json'
    },
    success: function(res) {
      callBack(res.data);
    },
    fail: function(error) {
      console.log(error);
    }
  })
}
// 演员表
function convertToCastString(casts) {
  var castsjoin = "";
  for (var idx in casts) {
    castsjoin = castsjoin + casts[idx].name + "/";
  }
  return castsjoin.substring(0, castsjoin.length - 1);
}
// 演员基本信息
function convertToCastInfos(casts) {
  var castsArry = [];
  for (var idx in casts) {
    var cast = {
      img: casts[idx].avatars ? casts[idx].avatars.large : "",
      name: casts[idx].name
    }
    castsArry.push(cast);
  }
  return castsArry;
}


// 输出数据
module.exports = {
  convertToStarsArray: convertToStarsArray,
  http: http,
  convertToCastString: convertToCastString,
  convertToCastInfos: convertToCastInfos
}